﻿using System;
using ServiceStack.Redis;
using ServiceStack.Redis.Generic;
using StackExchange.Redis;
using ServiceStack.Text;

namespace ConsoleApp
{
    public class demo_redis
    {
        public static void demo()
        {
            //Service_Stack_RedisClient();

            Stack_Exchange_RedisClient();
        }

        private static void Stack_Exchange_RedisClient()
        {

            /*
             * if we using master/slave pattern,just tell all the server address (it will automatically identify the master)
             */
            //ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("server1:6379,server2:6379");

            //ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("192.168.2.200");
            //IDatabase db = redis.GetDatabase();
            //while (true)
            //{
            //    long length = db.ListLength("bkjd_need_query");
            //    if (length > 0)
            //    {
            //        string ab = db.ListRightPop("bkjd_need_query");
            //        Console.WriteLine(ab);
            //    }
            //    else
            //    {
            //        Console.WriteLine("nothing");
            //        System.Threading.Thread.Sleep(1000 * 10 * 6);
            //    }
            //}
            //RedisKey key = "MBOA";
            //db.StringSet(key, "!11");
            //string a = db.StringGet(key);

            //ISubscriber sub = redis.GetSubscriber();
            //sub.Subscribe("messages", (channel, message) =>
            //{
            //    Console.WriteLine((string)message);
            //});
            //sub.Publish("messages", "hello，fuck");

            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("192.168.2.200");
        }

        private static void Service_Stack_RedisClient()
        {
            //using (RedisClient client = new RedisClient("192.168.2.200", 6379))
            //{             
            //    IRedisTypedClient<User> redis = client.As<User>();
            //    var bkjd_need_quey_list = redis.Lists["bkjd_need_query"];
            //    int i = 0;
            //    for (i = 0; i < int.MaxValue; i++)
            //    {
            //        var u = new User { Id = i, Name = "Name" + i };
            //        Console.WriteLine("生成一个:" + u.ToJson());
            //        redis.EnqueueItemOnList(bkjd_need_quey_list, u);
            //        if (i % 2 == 0)
            //        {
            //            System.Threading.Thread.Sleep(1000 * 10);
            //        }
            //        else
            //        {
            //            System.Threading.Thread.Sleep(1000 * 10 * 6);
            //        }
            //    }
            //}
            //return;


            using (RedisClient client = new RedisClient("192.168.2.200", 6379))
            {
                //if (client.ContainsKey("fucktest"))
                //{
                //    byte[] bdata = client.Get("fucktest");
                //    try
                //    {
                //        object obj = bdata.ToObject();
                //        var a = obj.GetType();
                //    }
                //    catch (Exception ex)
                //    {
                //        string msg = ex.Message;
                //    }
                //}
                //else
                //{

                //}
                //return;

                IRedisTypedClient<User> redis = client.As<User>();
                var bkjd_need_quey_list = redis.Lists["bkjd_need_query"];
                while (true)
                {
                    if (bkjd_need_quey_list.Count > 0)
                    {
                        User u = redis.DequeueItemFromList(bkjd_need_quey_list);
                        Console.WriteLine(u.Id + "," + u.Name);
                    }
                    else
                    {
                        Console.WriteLine("nothing to get");
                        System.Threading.Thread.Sleep(1000);
                    }
                }
            }

            return;
            using (RedisClient client = new RedisClient("192.168.2.200", 6379))
            {
                User user = new User
                {
                    Id = 12,
                    Name = "王相"
                };
                
                var isok = client.Remove("CommStrKey");
                User ur = client.Get<User>("CommStrKey");
                if (ur != null)
                {
                    string name = ur.Name;
                }
                else
                {
                    client.Set<User>("CommStrKey", user);
                }

                IRedisTypedClient<User> Users = client.As<User>();
                var auser = Users.GetValue("CommStrKey");
                if (auser != null)
                {

                }
            }
        }
    }

    public class User
    {
        public int Id { get; set; }
        public String Name { get; set; }
    }
}
